package com.ssh.manager.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ssh.common.util.Page;
import com.ssh.dao.IDao;
import com.ssh.entity.Activity;
import com.ssh.entity.Company;
import com.ssh.entity.Product;
import com.ssh.entity.SuperMarket;
import com.ssh.manager.ActivityManager;

@Service
public class ActivityManagerImpl implements ActivityManager {
	@Resource
	private IDao daoImpl;

	@Override
	public boolean delete(Long id) {
		try {
			Activity a = daoImpl.getDomain(Activity.class, id);
			a.setDel(1);
			daoImpl.saveOrUpdateDomain(a);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public void findList(Map conditionMap, Page page, String isDesc) {
		String sql = "select count(*) from Activity where del!=1";
		String findSql = "from Activity where del!=1";
		if(conditionMap.get("brand")!=null && !conditionMap.get("brand").equals("")){
			sql += " and brand.id="+conditionMap.get("brand");
			findSql += " and brand.id="+conditionMap.get("brand");
		}
		int sum = daoImpl.countAll(sql, null);
		page.setTotalCount(new Long(sum));
		page.countTotalPageNum();
		
		findSql += " order by createDate "+isDesc;
		List<Activity> activities = (List<Activity>)daoImpl.findListBySql(findSql, new Object[]{}, false, true, false, page.getNumberPerPage()*(page.getCurPageNum()-1), page.getNumberPerPage());
		page.setPageData(activities);
	}

	@Override
	public Activity saveUpdate(Activity activity) {
		return daoImpl.saveOrUpdateDomain(activity);
	}

	@Override
	public Activity getById(Long id) {
		return daoImpl.getDomain(Activity.class, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Activity> list() {	 
		return (List<Activity>)daoImpl.findListBySql("from Activity where del=? order by endDate desc,id desc", new Object[]{0}, false, true, false, 0, 30);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Company> companyList(long activityId) {
		// TODO Auto-generated method stub
		List<Object[]> list =(List<Object[]>) daoImpl.findListBySql("select  superMarket.company.id, superMarket.company.name  from ActivitySuperMarket where activity.id=? group by superMarket.company.id  order by id desc", new Object[]{activityId}, false, true, false, 0,-1);
		List<Company> listCompany=new ArrayList<Company>();
		for(Object[] obj:list){
			Company company=new Company();
			company.setId(Long.parseLong(obj[0].toString()));
			company.setName((String) obj[1]);
			listCompany.add(company);
		}
		return listCompany;
	}

	@Override
	public List<Map<String, String>> superMarketList(long activityId, long companyId) {
		// TODO Auto-generated method stub
		String sql = "select superMarket.id, superMarket.name,planHaibao,planDuitou,planToutao,planDuanjia,planChenlie,haibao,duitou,toutao,duanjia,chenlie  from ActivitySuperMarket ";
		sql += "where activity.id=? and superMarket.company.id=? and activity.endDate>? ";
		sql += "group by superMarket.id  order by id desc";
		List<Object[]> listResult =(List<Object[]>) daoImpl.findListBySql(sql, new Object[]{activityId,companyId,new Timestamp(System.currentTimeMillis())}, false, true, false, 0,-1);
		List<Map<String, String>> list=new ArrayList<Map<String, String>>();
		for(Object[] obj:listResult){
			Map<String, String> map=new HashMap<String, String>();
		    map.put("id", obj[0].toString());
		    map.put("name", obj[1].toString());
		    map.put("planHaibao", obj[2].toString());
		    map.put("planDuitou", obj[3].toString());
		    map.put("planToutao", obj[4].toString());
		    map.put("planDuanjia", obj[5].toString());
		    map.put("planChenlie", obj[6].toString());
		    map.put("haibao", obj[7].toString());
		    map.put("duitou", obj[8].toString());
		    map.put("toutao", obj[9].toString());
		    map.put("duanjia", obj[10].toString());
		    map.put("chenlie", obj[11].toString());
			list.add(map);
		}
		return list;
	}
	

	
}
